using System;
using System.Data;
using System.Collections;
using istIUniDB;

namespace Shraga.BLL
{
	[DBFunctions("Shraga_Candidate.Get_Candidacy", "Shraga_Candidate.Get_Candidacy")]
	public class Candidacy : DBObject
	{
		#region Private variables
		private Hashtable m_CandidateNode;
		#endregion

		#region Properties

		public int		 CandidacyId		{ get { return base.PrimaryKey; } }
		public int		 CandidateId		{ get { return base.GetIntData("ID_NUMBER"); } }		
		public int  	 PathId				{ get { return base.GetIntData("PATH_ID"); } }		
		public string	 PathName			{ get { return base.GetStringData("NAME"); } }
		public string	 Description		{ get { return base.GetStringData("DESCRIPTION"); } }
		public int	     StatusId			{ get { return base.GetIntData("STATUS"); } }		
		public String    Status				{ get { return base.GetStringData("STATUS_NAME"); }	}
		public DateTime  StartDate			{ get { return base.GetDateData("START_DATE"); } }
		public DateTime  EndDate			{ get { return base.GetDateData("END_DATE"); } }
		public DateTime  StatusChangeDate	{ get { return base.GetDateData("STATUS_DATE"); } }
		public new DateTime  LastUpdate		{ get { return base.LastUpdate; } }

		public Hashtable CandidateTrace		
		{ 
			get 
			{ 
				if (m_CandidateNode == null)
				{					
					IUniDB UniDB = UniDBSingleton.Instance().UniDB;			
		
					clsParameter[] arrParam = new clsParameter[1];
					arrParam[0] = new clsParameter(CandidacyId, OracleTypes.Int32);
					
					#region Load the CandidateNodes

					DataTable dtNodeCands = UniDB.RunStoredFunctionReturnDataTable("Shraga_Candidate_Node.Get_Candidacy_Nodes", ref arrParam);

					#region Populates Data

					m_CandidateNode = new Hashtable();
					foreach (DataRow drCurrent in dtNodeCands.Rows)
					{
						CandidateNode currCnd = 
							(CandidateNode)DBObjectsObserver.Instance().GetDBObject(typeof(CandidateNode), 
																	 Convert.ToInt32(drCurrent["PRIMARY_KEY"]), 
																	 new object[] { drCurrent} );
						currCnd.DBObjectUpdated += new dbobjectUpdated(SonDBObjectUpdated); 
						m_CandidateNode.Add(currCnd.CandidateNodeId, currCnd);
					}

					#endregion
				
					#endregion
				}

				return m_CandidateNode; 
			}
		}				


		#endregion

		#region Constructors
			
		public Candidacy(int CandidacyId) 
				: base(CandidacyId)
		{}

		public Candidacy(DataRow RawDataRow) 
			: base(RawDataRow)
		{}

		#endregion

		#region Public Static Candidacy Methods

		private void SonDBObjectUpdated(DBObject sender, DBObject updatedSon)
		{
			if (updatedSon == null)
			{
				base.PopEvent(sender);
			}
			else
			{
				base.PopEvent(updatedSon);
			}
		}

		public static bool CandidacyExists(int IdNumber, int PathId)
		{
			IUniDB UniDB = UniDBSingleton.Instance().UniDB;			
		
			clsParameter[] arrParam = new clsParameter[2];
			arrParam[0] = new clsParameter(IdNumber, OracleTypes.Int32);			
			arrParam[1] = new clsParameter(PathId, OracleTypes.Int32);			

			return Convert.ToBoolean(UniDB.RunStoredFunctionReturnInteger("Shraga_Candidate.Candidacy_Exists", ref arrParam));
		}

		public override void Refresh(Type MarshaledType, int MarshaledId)
		{
			if (MarshaledType == typeof(CandidateNode))
			{
				if ((m_CandidateNode != null) && (!m_CandidateNode.ContainsKey(MarshaledId)))
				{
					CandidateNode currCnd =(CandidateNode)DBObjectsObserver.Instance().GetDBObject(typeof(CandidateNode), 
						MarshaledId, 
						new object[] {MarshaledId});
					currCnd.DBObjectUpdated += new dbobjectUpdated(SonDBObjectUpdated); 					
					m_CandidateNode.Add(currCnd.CandidateNodeId, currCnd);				
					base.PopEvent(currCnd);
				}
			}
		}
		#endregion
	}
}